package com.example.sqlite;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText et_text;
    private ListView lv_customerList;
    private DataBaseHelp dbHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn_add = findViewById(R.id.btn_add);
        Button btn_paste = findViewById(R.id.btn_paste);

        et_text = findViewById(R.id.et_text);
        lv_customerList = findViewById(R.id.lv_customerList);

        dbHelper = new DataBaseHelp(MainActivity.this);
        showCustomersOnListView(dbHelper);
        // btn listener
        btn_add.setOnClickListener(view -> {
            CopyTextModel copyTextModel = new CopyTextModel();

            try {
                String text = et_text.getText().toString().trim();
                if (!text.equals("")) {
                    copyTextModel = new CopyTextModel();
                    copyTextModel.setId(-1);
                    copyTextModel.setText(text);
                }
            } catch (Exception e) {
                Toast.makeText(MainActivity.this, "输入的数据有误!",
                        Toast.LENGTH_SHORT).show();
            }

            boolean success = dbHelper.addOne(copyTextModel);
            if (!success) {
                Toast.makeText(MainActivity.this, "数据有误!",
                        Toast.LENGTH_SHORT).show();
            }
            et_text.setText("");

            showCustomersOnListView(dbHelper);
        });
        //paste
        btn_paste.setOnClickListener(view -> {
            // Gets a handle to the Clipboard Manager
            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

            String pasteString = clipboard.getPrimaryClip().getItemAt(0).getText().toString();

            StringBuffer sb = new StringBuffer(et_text.getText().toString());
            et_text.setText(sb.append(pasteString));
            et_text.setSelection(et_text.getText().length());

        });

        //delete
        lv_customerList.setOnItemLongClickListener((parent, view, position, id) -> {
            String text = parent.getItemAtPosition(position).toString();
            CopyTextModel copyTextModel = dbHelper.getClassByText(text);
            boolean success = dbHelper.deleteOne(copyTextModel);

            Toast.makeText(MainActivity.this, "删除成功",
                    Toast.LENGTH_SHORT).show();
            showCustomersOnListView(dbHelper);
            return success;
        });

        //copy
        lv_customerList.setOnItemClickListener((parent, view, position, id) -> {
            String text = parent.getItemAtPosition(position).toString();
            ClipboardManager clipboard = (ClipboardManager)
                    getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("simple text", text);
            clipboard.setPrimaryClip(clip);
            Toast.makeText(MainActivity.this, "复制成功",
                    Toast.LENGTH_SHORT).show();
        });
    }

    private void showCustomersOnListView(DataBaseHelp dbHelper) {
        ArrayAdapter<String> textArrayAdapter =
                new ArrayAdapter<>(MainActivity.this,
                        android.R.layout.simple_list_item_1,
                        dbHelper.getTextList());
        lv_customerList.setAdapter(textArrayAdapter);
    }
}